package com.prism.e4.rcp.ui.parts;

import javax.annotation.PostConstruct;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import swing2swt.layout.BoxLayout;

import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Group;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.gef.*;
import org.eclipse.swt.widgets.Text;

public class GraphicalEditor {

	private Label lblDragSource;
	private Text text;

	@PostConstruct
	public void createControls(Composite parent) {

		SashForm sashForm = new SashForm(parent, SWT.HORIZONTAL);

		ScrolledComposite scrolledComposite = new ScrolledComposite(sashForm,
				SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.setShowFocusedControl(true);
		scrolledComposite.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_WHITE));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);

		text = new Text(scrolledComposite, SWT.BORDER);
		text.setText("Drop Target");
		DropTarget dropTarget = new DropTarget(text, DND.DROP_DEFAULT
				| DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK);
		dropTarget.setTransfer(new Transfer[] { TextTransfer.getInstance() });
		dropTarget.addDropListener(new DropTargetAdapter() {
	      public void dragEnter(DropTargetEvent event) {
	        if (event.detail == DND.DROP_DEFAULT)
	          event.detail = DND.DROP_COPY;
	      }
	      public void dragOperationChanged(DropTargetEvent event) {
	        if (event.detail == DND.DROP_DEFAULT)
	          event.detail = DND.DROP_COPY;
	      }
	      public void drop(DropTargetEvent event) {
	        String operation = null;
	        switch (event.detail) {
	        case DND.DROP_MOVE:
	          operation = "moved";
	          break;
	        case DND.DROP_COPY:
	          operation = "copied";
	          break;
	        case DND.DROP_LINK:
	          operation = "linked";
	          break;
	        case DND.DROP_NONE:
	          operation = "disallowed";
	          break;
	        default:
	          operation = "unknown";
	          break;
	        }
	        text.append("\n" + operation + (String) event.data);
	      }
	    });

		scrolledComposite.setContent(text);
		scrolledComposite
				.setMinSize(text.computeSize(SWT.DEFAULT, SWT.DEFAULT));

		Group grpPalett = new Group(sashForm, SWT.BORDER);
		grpPalett.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		grpPalett.setText("Palettes");
		grpPalett.setLayout(new FillLayout(SWT.HORIZONTAL | SWT.VERTICAL));

		SashForm sashForm_1 = new SashForm(grpPalett, SWT.VERTICAL);

		Group grpPlaces = new Group(sashForm_1, SWT.BORDER | SWT.SHADOW_OUT);
		grpPlaces.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		grpPlaces.setText("Places");
		grpPlaces.setLayout(new FillLayout(SWT.HORIZONTAL));

		lblDragSource = new Label(grpPlaces, SWT.NONE);
		lblDragSource.setText("Drag Source");
		DragSource dragSource = new DragSource(lblDragSource, DND.DROP_COPY
				| DND.DROP_MOVE | DND.DROP_LINK);
		dragSource.setTransfer(new Transfer[] { TextTransfer.getInstance() });
		dragSource.addDragListener(new DragSourceAdapter() {
			public void dragSetData(DragSourceEvent event) {
				event.data = "Text Transferred";
			}

			public void dragFinished(DragSourceEvent event) {
				if (event.doit) {
					String operation = null;
					switch (event.detail) {
					case DND.DROP_MOVE:
						operation = "moved";
						break;
					case DND.DROP_COPY:
						operation = "copied";
						break;
					case DND.DROP_LINK:
						operation = "linked";
						break;
					case DND.DROP_NONE:
						operation = "disallowed";
						break;
					default:
						operation = "unknown";
						break;
					}
					lblDragSource.setText("Drag Source (data " + operation + ")");
				} else {
					lblDragSource.setText("Drag Source (drag cancelled)");
				}
			}
		});

		Group grpTransitions = new Group(sashForm_1, SWT.BORDER | SWT.SHADOW_IN);
		grpTransitions.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_WHITE));
		grpTransitions.setText("Transitions");
		grpTransitions.setLayout(new FillLayout(SWT.HORIZONTAL));

		DragSource dragSource_1 = new DragSource(grpTransitions, DND.DROP_MOVE);

		Group grpConnections = new Group(sashForm_1, SWT.BORDER
				| SWT.SHADOW_NONE);
		grpConnections.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_WHITE));
		grpConnections.setText("Connections");
		grpConnections.setLayout(new FillLayout(SWT.HORIZONTAL));

		DragSource dragSource_2 = new DragSource(grpConnections, DND.DROP_MOVE);
		sashForm_1.setWeights(new int[] { 128, 96, 116 });
		sashForm.setWeights(new int[] { 393, 202 });
	}
}
